package com.cnpc.iot.scgl.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.utils.StrUtil;
import com.cnpc.iot.scgl.entity.Pnode;
import com.cnpc.iot.scgl.service.PnodeService;

/**
 * 流程节点管理控制器
 *
 * @author jrn 2018-03-19 22:26:09由代码生成器自动生成
 */
@Controller
@RequestMapping("/pnode")
public class PnodeController {

    @Resource
    private BaseService baseService;
    @Autowired
    private PnodeService pnodeService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "iot/scgl/pnode_list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(String id, String flowId, HttpServletRequest request) {
        if (StringUtils.isEmpty(id)) {
            request.setAttribute("flowId", flowId);
        }else {
        	request.setAttribute("id", id);
        	Pnode pnode = baseService.get(Pnode.class, id);
        	request.setAttribute("flowId", pnode.getFlowId());
        }
        return "iot/scgl/pnode_edit";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(String id, HttpServletRequest request) {
        request.setAttribute("id", id);
        return "iot/scgl/pnode_detail";
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Pnode get(@PathVariable("id") String id) {
        return baseService.get(Pnode.class, id);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(Pnode pnode) {
        if (StrUtil.isEmpty(pnode.getId())) {
        	String id = (String) baseService.save(pnode);
        	System.out.println(id);
        } else {
            pnode.setUpdateDateTime(new Date());
            baseService.update(pnode);
        }
        return new Result(true);
    }


    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id) {
        Pnode pnode = this.get(id);
        try {
            baseService.delete(pnode);
            return new Result(true);
        } catch (Exception e) {
            return new Result(false, "该数据已经被引用，不可删除");
        }
    }

    @RequestMapping(value = "/sync_table/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result syncTable(@PathVariable("id") String id) {
        Pnode pnode =  baseService.get(Pnode.class, id);
        if(null != pnode){
            boolean flag = pnodeService.syncTable(pnode);
            if(flag){
                return new Result(true);
            }
        }
        return new Result(false);
    }

    @RequestMapping(value = "/getPnodeByFlow/{flowId}", method = RequestMethod.POST)
    @ResponseBody
    public List<Pnode> getPnodeByFlow(@PathVariable("flowId") String flowId){
    	List<Pnode> list = baseService.find("from Pnode where flowId= '"+flowId + "' order by orderId");
    	return list;
    }
    
}
